package com.rocksea.app.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.widget.TextView;

import com.rocksea.app.R;
import com.rocksea.app.db.history.HistoryTable;
import com.rocksea.app.fragment.CommonBaseInfoFragment;
import com.rocksea.app.fragment.PhotoFragment;
import com.rocksea.app.fragment.SetionInfoFragment;
import com.rocksea.app.fragment.SetionViewFragment;
import com.rocksea.app.interfaces.MyExecuteCallBack;
import com.rocksea.app.tasks.QueryPileTask;
import com.rocksea.app.tasks.Temp;
import com.rocksea.app.utils.UiUtils;


/**
 *
 * @author 吴志华
 * @date 2017/10/26
 *
 * @FileName:com.rocksea.app.activity.SonicWaveActivity.java
 */

public class SonicWaveActivity extends BaseShowActivity {
    private static String[] TITLES = {"基本信息","测点数据","有效波形","无效波形","现场照片"};

    private boolean isLoadScuess = false;
    private View mErrorView;
    private View mProgressBarView;
    private TextView mErrorText;

    @Override
    public void addFragment(ViewPager viewPager)
    {
        if (!isLoadScuess)
            return;
        CommonFragmentAdapter lowStrainFragmentAdapter = new CommonFragmentAdapter(getSupportFragmentManager());
        viewPager.setAdapter(lowStrainFragmentAdapter);
    }
    @Override
    protected void actionBarTitle(ActionBar actionBar)
    {
        actionBar.setTitle(String.format("%s-声测信息",infos[1]));
    }
    @Override
    public void initData()
    {
        String baseInfoId = infos[2];
        mBundle = new Bundle();
        mBundle.putString(BaseShowActivity.BASEINFOID_KEY,baseInfoId);
        mBundle.putString(BaseShowActivity.PLAN_NUM_KEY,infos[0]);
        mBundle.putString(BaseShowActivity.PLIE_NO_KEY,infos[1]);
        loading();
        QueryPileTask queryPileTask = new QueryPileTask(baseInfoId,callBack);
        queryPileTask.execute();
    }

    @Override
    protected void initView()
    {
        super.initView();
        mErrorView = findViewById(R.id.Error_Layout);
        mProgressBarView = findViewById(R.id.ProgressBar);
        mErrorText = (TextView) findViewById(R.id.Error_Text);
        mErrorText.setOnClickListener(onClickListener);
    }

    private void loading()
    {
        mViewPager.setVisibility(View.GONE);
        mErrorView.setVisibility(View.GONE);
        mProgressBarView.setVisibility(View.VISIBLE);
    }

    private void loadCompleted()
    {
        mViewPager.setVisibility(View.VISIBLE);
        mErrorView.setVisibility(View.GONE);
        mProgressBarView.setVisibility(View.GONE);
    }

    public void loadFailed()
    {
        mErrorText.setText("加载失败");
        mErrorView.setVisibility(View.VISIBLE);
        mProgressBarView.setVisibility(View.GONE);
        mViewPager.setVisibility(View.GONE);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            initData();
        }
    };


    private class CommonFragmentAdapter extends FragmentPagerAdapter {
        private CommonFragmentAdapter(FragmentManager fm)
        {
            super(fm);
        }
        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    mBundle.putInt(CommonBaseInfoFragment.MODE, HistoryTable.TEST_SC);
                    return CommonBaseInfoFragment.newInstance(mBundle);
                case 1:
                    mBundle.putInt(SetionViewFragment.VALID_KEY,-1);
                    return SetionInfoFragment.newInstance(mBundle);
                case 2:
                    mBundle.putInt(SetionViewFragment.VALID_KEY,0);
                    return SetionInfoFragment.newInstance(mBundle);
                case 3:
                    mBundle.putInt(SetionViewFragment.VALID_KEY,1);
                    return SetionInfoFragment.newInstance(mBundle);
                case 4:
                    return PhotoFragment.newInstance(mBundle);
                default:
                    return null;
            }
        }
        @Override
        public int getCount() {
            return TITLES.length;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

    }

    private MyExecuteCallBack<String[]> callBack = new MyExecuteCallBack<String[]>() {
        @Override
        public void onError(int code, String message)
        {
            UiUtils.showToast(String.format("%s(%d)",message,code));
            loadFailed();
        }
        @Override
        public void onSuccess(String[] strings)
        {
            loadCompleted();
            isLoadScuess= true;
            addFragment(mViewPager);
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Temp.clear();
    }
}
